package com.itxiuyixiu.homework.utils.error.handlers;

import com.itxiuyixiu.homework.utils.ReturnUtils.ReturnMessage;
import com.itxiuyixiu.homework.utils.ReturnUtils.ReturnVo;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.validation.ConstraintViolationException;

/**
 * @author 黄磊
 * @date 2020/1/13
 */
@ControllerAdvice
public class ParamExceptionHandlers {
    /**
     * 参数缺失与参数验证失败的全局处理方法
     *
     * @param ex 异常
     * @return 返回信息
     */
    @ResponseBody
    @ExceptionHandler(value = {MissingServletRequestParameterException.class, ConstraintViolationException.class})
    public ReturnVo errorHandler(Exception ex) {
        return ReturnMessage.getFailureReturn(ex.getMessage());
    }
}
